﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sudoku.Game;
using Sudoku.ViewHelper;

namespace Sudoku
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, IView
    {
    	private TextBox[,] _textBoxs;
    	private Dictionary<int, StackPanel> _stacks;
    	private TextBox _focusElement;

        public MainWindow()
        {
            InitializeComponent();

        	FillStacks();

        	MakeGrid();

        	var pre = new GamePresenter(this);
        }

		private void FillStacks()
		{
			_stacks = new Dictionary<int, StackPanel>
			          	{
			          		{0, stackPanel1},
			          		{1, stackPanel2},
			          		{2, stackPanel3},
			          		{3, stackPanel4},
			          		{4, stackPanel5},
			          		{5, stackPanel6},
			          		{6, stackPanel7},
			          		{7, stackPanel8},
			          		{8, stackPanel9}
			          	};
		}

		private void MakeGrid()
		{
			_textBoxs = new TextBox[9,9];




			for (int i = 0; i < 9; i++)
				for (int j = 0; j < 9; j++)
				{
					_textBoxs[i, j] = new TextBox
					                  	{
					                  		FontSize = 26,
					                  		Width = 40,
					                  		Height = 40,
					                  		TextAlignment = TextAlignment.Center,
											FontStyle = FontStyles.Normal,
											FontWeight = FontWeights.Thin
					                  	};
					_textBoxs[i, j].GotFocus += new RoutedEventHandler(MainWindow_GotFocus);

					_stacks[i].Children.Add(_textBoxs[i, j]);
				}

		}

    	void MainWindow_GotFocus(object sender, RoutedEventArgs e)
		{
			if (sender is TextBox)
				_focusElement = sender as TextBox;
		}

		public event EventHandler<EventArgs> NewGame;

		public event EventHandler<EventArgs> ClearUserData;

		public event EventHandler<EventArgs> SendSolution;

		public event EventHandler<EventArgs> HelpSolution;

		public FieldState GetCurrentState()
		{
			var res = new FieldState();

			try
			{
				for (int i = 0; i < 9; i++)
					for (int j = 0; j < 9; j++)
					{
						res.Field[i, j] = new Cell(0,false);
						res.Field[i, j].Value = Convert.ToInt32(_textBoxs[i, j].Text);
					}
			}
			catch
			{
				res = null;
				ShowMessage("Не удалось распознать символы введеные в игровое поле");
			}

			return res;
		}

		public void SetCurrentState(FieldState fieldState)
		{
			for (int i = 0; i < 9; i++)
				for (int j = 0; j < 9; j++)
				{
					if (fieldState.Field[i, j] == null)
					{
						_textBoxs[i, j].Text = "";
						_textBoxs[i, j].FontWeight = FontWeights.Thin;
						_textBoxs[i, j].FontStyle = FontStyles.Normal;
					}
					else
					{
						_textBoxs[i, j].Text = fieldState.Field[i, j].Value.ToString();
						if (fieldState.Field[i, j].ProgramGenerated)
						{
							_textBoxs[i, j].FontWeight = FontWeights.SemiBold;
							_textBoxs[i, j].FontStyle = FontStyles.Italic;
						}
						else
						{
							_textBoxs[i, j].FontWeight = FontWeights.Thin;
							_textBoxs[i, j].FontStyle = FontStyles.Normal;
						}
					}
				}
		}

		public Cell GetCellState(int horizontal, int vertical)
		{
			throw new NotImplementedException();
		}

		public void SetCellState(int horizontal, int vertical, Cell value)
		{
			if (value == null)
				return;

			_textBoxs[vertical, horizontal].Text = value.Value.ToString();
			if (value.ProgramGenerated)
			{
				_textBoxs[vertical, horizontal].FontWeight = FontWeights.SemiBold;
				_textBoxs[vertical, horizontal].FontStyle = FontStyles.Italic;
			}

			else
			{
				_textBoxs[vertical, horizontal].FontWeight = FontWeights.Thin;
				_textBoxs[vertical, horizontal].FontStyle = FontStyles.Normal;
			}
		}

    	public GameDifficulty GetSettedDifficulty()
		{
			return SliderHelper.GetDifficulty((int)slider1.Value);
		}

		public Coordinate GetActiveCellCoordinate()
		{
			return GetActivTextB();
		}

		private Coordinate GetActivTextB()
		{
			for (int i = 0; i < 9; i++)
				for (int j = 0; j < 9; j++)
				{
					if (_textBoxs[i,j] == _focusElement)
						return new Coordinate
						       	{
						       		Horizontal = j,
									Vertical = i
						       	};
				}
			return null;
		}

    	public void ShowMessage(string msg)
    	{
    		MessageBox.Show(msg);
    	}

		private void NewGame_Click(object sender, RoutedEventArgs e)
		{
			if (NewGame != null)
				NewGame(sender, e);
		}

		private void Clear_Click(object sender, RoutedEventArgs e)
		{
			if (ClearUserData != null)
				ClearUserData(sender, e);
		}

		private void Help_Click(object sender, RoutedEventArgs e)
		{
			if (HelpSolution != null)
				HelpSolution(sender, e);
		}

		private void button3_Click(object sender, RoutedEventArgs e)
		{
			if (SendSolution != null)
				SendSolution(sender, e);
		}

		private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			txtDifficulty.Text = SliderHelper.GetDefenition((int) slider1.Value);
		}
	}
}
